package cn.lang.controller;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.StrUtil;
import cn.lang.oss.handler.OssQnyHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
@RestController
public class TestController {

    @Autowired
    private OssQnyHandler ossHelloHandler;

    @RequestMapping("/test2")
    public String updateMd(String dir) throws IOException {
        dir = "D://JavaGuide-main/";
        Path path= Paths.get(dir);
        Files.walkFileTree(path,new FileVisitor<Path>(){

            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
               // log.info("读取到目录：{}",dir.getFileName());
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                //log.info("读取到文件：{}",path.getFileName());
                if (!StrUtil.endWith(path.getFileName().toString(),".md",true)){
                    return FileVisitResult.CONTINUE;
                }
                File file = path.toFile();
                String path1 = file.getPath();
                String[] split = file.getPath().split("\\\\");
                String replace = path1.replace(split[split.length - 1], "");
                FileReader fileReader = new FileReader(file);
                String readString = fileReader.readString();
                String reg = "!\\[.*\\]\\(([^http].*[\\.png|\\.jpg|\\.jepg|\\.drawio])\\)";
                Matcher m = Pattern.compile(reg).matcher(readString);
                while (m.find()) {
                    String group = m.group(1);
                    String s = replace + group;
                    System.out.println(s);
                    File file2 = new File(s);
                    String upload = ossHelloHandler.upload(file2,group.replace("./",""));
                    if (StrUtil.isNotBlank(upload)){
                        String url = ossHelloHandler.getUrl(upload);
                        System.out.println(url);
                        readString = readString.replace(group,url);
                    }
                }
                FileWriter writer = new FileWriter(file);
                writer.write(readString);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        });
        return "ok";
    }

    @RequestMapping("/test")
    public void tes(){
        File file = new File("D:\\JavaGuide-main\\docs\\java\\collection\\images\\java7_concurrenthashmap.drawio");
        String upload = ossHelloHandler.upload(file);
    }

    public static void main(String[] args) throws IOException {
        TestController testController = new TestController();
        String s = "D:\\JavaGuide-main\\docs\\system-design\\security\\./images/sentive-words-filter/hutool-dfa.png";
        File file = new File(s);
        FileReader fileReader = new FileReader(file);
        String readString = fileReader.readString();

    }

}
